package com.beagle.njddapp.core;

import com.amap.api.location.AMapLocation;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hxx
 * @date 2021-03-13 20:39
 * 位置信息订阅发布类
 */
public class LocationManager  {

    private List<LocationChangeLisenter> lisenterList;
    private double longitude;//经度
    private double latitude;//weidu
    public  AMapLocation location;



    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    private LocationManager(){
        lisenterList=new ArrayList<>();
    }
    public void addLisenter(LocationChangeLisenter locationChangeLisenter){
        lisenterList.add(locationChangeLisenter);
    }

    public void pubLishLocation(AMapLocation aMapLocation){
        location=aMapLocation;
        longitude=aMapLocation.getLongitude();
        latitude=aMapLocation.getLatitude();
        for (LocationChangeLisenter locationChangeLisenter:lisenterList){
            locationChangeLisenter.onLocationChanged(aMapLocation);
        }
    }



    public  static LocationManager getInstance(){
        return Singletion.instance;
    }

    public String getCityCode() {
        if (location!=null){
            return  location.getCityCode();
        }
        return "";
    }

    public static class Singletion{

        private static LocationManager instance = new LocationManager();

    }


    /**
     * 获取具体位置str
     */
    public  String getLocation(){
        if (location==null){
            return "";
        }
        StringBuilder sb=new StringBuilder();
//        sb.append(location.getProvince());
//        sb.append(location.getCity());
//        sb.append(location.getDistrict());
        sb.append(location.getAddress());
        return sb.toString();
    }

    public  String getProvince(){
        if (location==null){
            return "";
        }
        return location.getProvince();
    }
    public  String getCity(){
        if (location==null){
            return "";
        }
        return location.getCity();
    }
    public  String getDistrict(){
        if (location==null){
            return "";
        }
        return location.getDistrict();
    }







    public interface LocationChangeLisenter{
        void onLocationChanged(AMapLocation location);
    }
}
